#ifndef TYPES_H
#define TYPES_H

#include "ZTypes.h"

/********* General variables *********/
#define TICKS_PER_SECOND 50000000
#define THREAD_SUSPEND_TIME 5000

typedef enum
{
	SUCCESS,
	GENERAL_ERROR,
	ILLEGAL_PARAM
} error_t;

#define PIN_OFF 0x00
#define PIN_ON	0x01

/********* Control Settings *********/

// Temperature type
typedef int temp_t;

// Thermostat HVAC modes
typedef enum
{
	OFF,
	HEAT,
	AC
} hvac_mode_t;

// State of the HVAC system
typedef enum
{
	IDLE,
	HEATING,
	COOLING
} hvac_state_t;

// Fan modes
typedef enum
{
	FAN_AUTO,
	FAN_ON
} fan_mode_t;
 
// Fan state
typedef enum
{
	FAN_IDLE,
	FAN_RUNNING
} fan_state_t;

// Heater types
typedef enum
{
	GAS,
	ELECTRIC
} heater_t;

// Temperature modes
typedef enum
{
	CELSIUS,
	FAHRENHEIT
} temp_unit_t;

// Operation mode
typedef enum
{
	SCHEDULE_ACTIVE,
	HOLD
} operation_mode_t;

#define NUM_SCHEDULE_DAYS 7

/* Scheduling Types */
#define MAX_DAILY_SCHED_CHANGES 4

#define END_SCHED_TERMINATOR_TEMP 0

typedef int sched_time_t;

// This "object" represents a schedule change point
typedef struct
{
	temp_t 			temp;
	sched_time_t 	time;	// Given in minutes (0-1440) representing the number of minutes past midnight
} sched_change_point_t;

// A single day in the schedule contains an array of sched_change_point_t's
typedef struct
{
	sched_change_point_t 	changes[MAX_DAILY_SCHED_CHANGES];
} sched_day_t;

// Type definition for time zone
// This enum value is also the number of hours to add to UTC to get to local US standard times
typedef enum
{
	EASTERN 	= -5,
	CENTRAL 	= -6,
	MOUNTAIN 	= -7,
	PACIFIC		= -8
} time_zone_t;

// For future use with history logging
typedef enum
{
	DAY,
	WEEK,
	MONTH,
	YEAR,
	ALL_TIME
} duration_t;

// WLAN settings
typedef struct
{
	UINT8 macAddr[6];
	UINT8 dhcpEnabled;
	UINT32 ipAddr;
	UINT32 inetMask;
	UINT32 gatewayAddr;
	UINT32 dnsAddr;
	char ssid[33];
	UINT8 wepKey[13];
	UINT8 wepKeyLen;
} wlan_settings_t;

// HVAC settings
typedef struct
{
	// The desired temperature
	temp_t setTemp;

	// The allowable deviation from the setTemp
	temp_t tempVariance;

	// The mode of the HVAC setting (Off, Heat, A/C)
	hvac_mode_t setHVACMode;

	// The mode of the fan setting (Auto, On)
	fan_mode_t setFanMode;

	heater_t myHeaterType;

	// The temperature units (Fahrenheit or Celsius)
	temp_unit_t tempUnit;

	// The operation mode of the system (either the schedule is active or in hold mode)
	operation_mode_t opMode;

	// The default polling interval
	TIME_t pollInterval;
} hvac_settings_t;

#define MAX_TIME_SERVER_CHARS 50

typedef struct
{
	BOOL internetTimeEnabled;
	char timeServerAddress[MAX_TIME_SERVER_CHARS];
	BOOL daylightSavingEnabled;
	time_zone_t timeZone;
} time_settings_t;

typedef struct
{
	TIME_t ACRuntime;
	TIME_t HeatRuntime;
	TIME_t FanRuntime;
} history_log_t;

// Only using ALL_TIME logs for now
// Open for future addition of different durations
typedef struct
{
	history_log_t allTimeLog;
} history_settings_t;

// Settings stored to persisten memory
// this struct must be kept below 2K (the size of a page)
typedef struct {
	wlan_settings_t 	wlanSettings;
	hvac_settings_t 	hvacSettings;
	sched_day_t 		schedule[NUM_SCHEDULE_DAYS];
	time_settings_t 	timeSettings;
	temp_t				tempOffset;
} eeprom_settings_t;

#endif
